﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.View.CustomerManage
{
    public partial class Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                btnSave.Attributes.Add("OnClick", "return  ValidatorAll()");
                if (Request["id"] != null)
                {
                    string customer_id = Request["id"].Trim();
                    customer.Value = customer_id;
                    BindDate(customer_id);
                    btnSave.Text = "编 辑";
                }
            }
        }

        protected void BindDate(string customer_id)
        {
            Model.View.T_PUB_CUSTOMERINFO model = new Model.View.T_PUB_CUSTOMERINFO();
            BLL.View.T_PUB_CUSTOMERINFO bll = new BLL.View.T_PUB_CUSTOMERINFO();
            model = bll.GetModel(customer_id);
            txtBRIDE_NAME.Value = model.BRIDE_NAME;
            txtBRIDEADDRESS.Text = model.BRIDEADDRESS;
            txtBRIDEAGE.Value = model.BRIDEAGE.ToString();
            txtBRIDEPHONE.Value = model.BRIDEPHONE;
            txtBRIDEPOST.Text = model.BRIDEPOST;
            txtBRIDEQQ.Value = model.BRIDEQQ;
            txtGROOM_NAME.Value = model.GROOM_NAME;
            txtGROOMAGE.Value = model.GROOMAGE.ToString();
            txtGROOMPHONE.Value = model.GROOMPHONE;
            txtGROOMPOST.Text = model.GROOMPOST;
            txtGROOMQQ.Value = model.GROOMQQ;
            txtHOUSEADDRESS.Text = model.HOUSEADDRESS;
            //以下信息可以通过tbody控制
            txtMARRIAGECAR.Text = model.MARRIAGECAR;
            txtRANK.Text = model.RANK.ToString();
            txtTABLENUM.Text = model.TABLENUM.ToString();
            d12.Value = Convert.ToDateTime(model.TIME).ToShortDateString().Replace('/', '-');
            txtWEDDING.Text = model.WEDDING;
            txtYOUNGER.Text = model.YOUNGER.ToString();
            txtELDER.Text = model.ELDER.ToString();
            txtHOTEL.Text = model.HOTEL;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.View.T_PUB_CUSTOMERINFO model = new Model.View.T_PUB_CUSTOMERINFO();
            BLL.View.T_PUB_CUSTOMERINFO bll = new BLL.View.T_PUB_CUSTOMERINFO();
            if (customer.Value.ToString() != "")
            {
                model = bll.GetModel(customer.Value);
                model.BRIDE_NAME = txtBRIDE_NAME.Value.Trim();
                model.BRIDEADDRESS = txtBRIDEADDRESS.Text.Trim();
                if (txtBRIDEAGE.Value.Trim() != "")
                {
                    model.BRIDEAGE = int.Parse(txtBRIDEAGE.Value.Trim());
                }
                model.BRIDEPHONE = txtBRIDEPHONE.Value.Trim();
                model.BRIDEPOST = txtBRIDEPOST.Text.Trim();
                model.BRIDEQQ = txtBRIDEQQ.Value.Trim();
                model.GROOM_NAME = txtGROOM_NAME.Value.Trim();
                if (txtGROOMAGE.Value.Trim() != "")
                {
                    model.GROOMAGE = int.Parse(txtGROOMAGE.Value.Trim());
                }
                model.GROOMPHONE = txtGROOMPHONE.Value.Trim();
                model.GROOMPOST = txtGROOMPOST.Text.Trim();
                model.GROOMQQ = txtGROOMQQ.Value.Trim();
                model.HOUSEADDRESS = txtHOUSEADDRESS.Text.Trim();
                //以下信息可以通过tbody来控制是否显示
                model.MARRIAGECAR = txtMARRIAGECAR.Text.Trim();
                if (txtRANK.Text.Trim() != "")
                {
                    model.RANK = Convert.ToInt32(txtRANK.Text.Trim());
                }
                if (txtTABLENUM.Text.Trim() != "")
                {
                    model.TABLENUM = Convert.ToInt32(txtTABLENUM.Text.Trim());
                }
                if (d12.Value != "")
                {
                    model.TIME = Convert.ToDateTime(d12.Value);
                }
                model.WEDDING = txtWEDDING.Text.Trim();
                if (txtYOUNGER.Text.Trim() != "")
                {
                    model.YOUNGER = Convert.ToInt32(txtYOUNGER.Text.Trim());
                }
                if (txtELDER.Text.Trim() != "")
                {
                    model.ELDER = Convert.ToInt32(txtELDER.Text.Trim());
                }
                model.HOTEL = txtHOTEL.Text.Trim();
                bool flag = bll.Update(model);
                if (flag == true)
                {
                    Response.Redirect("CustomerList.aspx");
                }
                else
                {
                    Universal.JS.Alert("编辑失败！请您重新填写！");
                }
            }
            else
            {
                model.CUSTOMER_ID = System.Guid.NewGuid().ToString();
                model.BRIDE_NAME = txtBRIDE_NAME.Value.Trim();
                model.BRIDEADDRESS = txtBRIDEADDRESS.Text.Trim();
                if (txtBRIDEAGE.Value.Trim() != "")
                {
                    model.BRIDEAGE = int.Parse(txtBRIDEAGE.Value.Trim());
                }
                model.BRIDEPHONE = txtBRIDEPHONE.Value.Trim();
                model.BRIDEPOST = txtBRIDEPOST.Text.Trim();
                model.BRIDEQQ = txtBRIDEQQ.Value.Trim();
                model.GROOM_NAME = txtGROOM_NAME.Value.Trim();
                if (txtGROOMAGE.Value.Trim() != "")
                {
                    model.GROOMAGE = int.Parse(txtGROOMAGE.Value.Trim());
                }
                model.GROOMPHONE = txtGROOMPHONE.Value.Trim();
                model.GROOMPOST = txtGROOMPOST.Text.Trim();
                model.GROOMQQ = txtGROOMQQ.Value.Trim();
                model.HOUSEADDRESS = txtHOUSEADDRESS.Text.Trim();
                //以下信息可以通过tbody来控制是否显示
                model.MARRIAGECAR = txtMARRIAGECAR.Text.Trim();
                if (txtRANK.Text.Trim() != "")
                {
                    model.RANK = Convert.ToInt32(txtRANK.Text.Trim());
                }
                if (txtTABLENUM.Text.Trim() != "")
                {
                    model.TABLENUM = Convert.ToInt32(txtTABLENUM.Text.Trim());
                }
                if (d12.Value != "")
                {
                    model.TIME = Convert.ToDateTime(d12.Value);
                }
                model.WEDDING = txtWEDDING.Text.Trim();
                if (txtYOUNGER.Text.Trim() != "")
                {
                    model.YOUNGER = Convert.ToInt32(txtYOUNGER.Text.Trim());
                }
                if (txtELDER.Text.Trim() != "")
                {
                    model.ELDER = Convert.ToInt32(txtELDER.Text.Trim());
                }
                model.HOTEL = txtHOTEL.Text.Trim();
                bool flag = bll.Add(model);
                if (flag == true)
                {
                    Response.Redirect("CustomerList.aspx");
                }
                else
                {
                    Universal.JS.Alert("保存失败！请您重新填写！");
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerList.aspx");
        }
    }
}